from app.core.img_opencv import *
from app.work.work_public import *

# region 宝石合成
def gemstone_work(check_boxes,scale):
    """
    宝石合成过程
    """
    def click_synthesis(num,scale):
        """
        内置点击合成函数
        """
        click_image('app_img/synthesis_gemstone/5.png',image_num=num, threshold=0.7, scale=scale)
        if (if_exist_click_image('app_img/public_img/yes.png', threshold=0.7, scale=scale)):
            wait_click_image('app_img/public_img/get_reward.png', threshold=0.7, scale=scale)

    input_list = []
    for check_box in check_boxes:
        input_list.append(check_box.isChecked())
    wait_click_image('app_img/synthesis_gemstone/2.png', threshold=0.7, scale=scale)
    wait_click_image('app_img/synthesis_gemstone/3.png', threshold=0.7, scale=scale)
    wait_click_image('app_img/synthesis_gemstone/4.png', threshold=0.7, scale=scale)
    wait_image('app_img/synthesis_gemstone/5.png', threshold=0.7, scale=scale)

    queue=find_grid_patterns('app_img/synthesis_gemstone/5.png', threshold=0.7, scale=scale)

    # 合成逻辑
    for i in range(7):
        if input_list[i]:
            click_synthesis(1,scale)
        mouse_drag(queue[1][0],queue[1][1],queue[0][0],queue[0][1])

    for i in range(5):
        if input_list[i+7]:
            click_synthesis(i+1,scale)

# region 助战
def zhuzhan_work(kunrun_map_num,scale,key_map):
    """
    助战过程
    """
    wait_click_image('app_img/get_zhuzhan_coins/protect_kunrun.png', threshold=0.7, scale=scale)
    if_exist_click_image('app_img/get_zhuzhan_coins/2.png', threshold=0.95, scale=scale)
    pos1=find_grid_patterns('app_img/get_zhuzhan_coins/3_1.png', threshold=0.7, scale=scale)
    pos2=find_grid_patterns('app_img/get_zhuzhan_coins/3_2.png', threshold=0.7, scale=scale)
    for _ in range(4):
        mouse_drag(pos1[0][0], pos1[0][1], pos2[0][0], pos2[0][1], duration=0.3)
        time.sleep(0.2)

    if(kunrun_map_num==1):
        click_image('app_img/get_zhuzhan_coins/map_1.png', threshold=0.7, scale=scale)
    elif(kunrun_map_num==2):
        click_image('app_img/get_zhuzhan_coins/map_2.png', threshold=0.7, scale=scale)
    elif(kunrun_map_num==3):
        click_image('app_img/get_zhuzhan_coins/map_3.png', threshold=0.7, scale=scale)
    
    click_image('app_img/get_zhuzhan_coins/4.png', threshold=0.7, scale=scale)
    mouse_moveto(1,1)
    num=0
    while True:
        wait_click_image('app_img/get_zhuzhan_coins/5.png', threshold=0.8, scale=scale)
        wait_image('app_img/get_zhuzhan_coins/7.png', threshold=0.7, scale=scale)
        while True:
            if not is_image_now('app_img/get_zhuzhan_coins/5_1.png', threshold=0.8, scale=scale):
                break
        chat_with_other(text='如果好了，请准备')
        click_image('app_img/get_zhuzhan_coins/5_2.png', threshold=0.7, scale=scale)
        img_list=['app_img/get_zhuzhan_coins/5_1.png','app_img/get_zhuzhan_coins/6_1.png','app_img/get_zhuzhan_coins/6_2.png']
        wait_many_image(img_list, threshold=0.7, scale=scale)
        if is_image_now('app_img/get_zhuzhan_coins/5_1.png', threshold=0.7, scale=scale):
            wait_click_image('app_img/get_zhuzhan_coins/14.png', threshold=0.7, scale=scale)
            continue    
        if_exist_click_image('app_img/get_zhuzhan_coins/6_2.png', threshold=0.7, scale=scale)
        
        click_image('app_img/get_zhuzhan_coins/7.png', threshold=0.7, scale=scale)
        if_exist_click_image('app_img/public_img/yes.png', threshold=0.7, scale=scale)
        if_exist_click_image('app_img/get_zhuzhan_coins/8.png', threshold=0.7, scale=scale)
        wait_image('app_img/get_zhuzhan_coins/9.png', threshold=0.7, scale=scale)
        click_image('app_img/get_zhuzhan_coins/10.png', threshold=0.7, scale=scale)

        chat_with_other(text='如果准备好了,请点击下一波')

        for _ in range(65):
            if if_exist_click_image('app_img/get_zhuzhan_coins/11.png', threshold=0.7, scale=scale):
                break
            time.sleep(0.8)
        chat_with_other(text='挂机中...')
        time.sleep(1)
        chat_with_other(text='B站、抖音、快手关注你在教我做人')
        wait_image('app_img/get_zhuzhan_coins/11_1.png', threshold=0.7, scale=scale)

        for _ in range(18):
            if if_exist_click_image('app_img/get_zhuzhan_coins/11.png', threshold=0.7, scale=scale):
                break
            time.sleep(0.8)
        time.sleep(5)
        while True:
            if if_exist_click_image('app_img/get_zhuzhan_coins/12.png', threshold=0.7, scale=scale):
                break
            short_press(key=key_map['key_k'])
            short_press(key=key_map['key_attack'])
            time.sleep(2)
        wait_click_image('app_img/get_zhuzhan_coins/14.png', threshold=0.7, scale=scale)
        num+=1
        if num==34:
            break

# region 斗宠反击
def dc_counterattack_work(scale):
    """
    斗宠反击过程
    """
    while True:
        wait_click_image('app_img/dcfj_challenge/3.png', threshold=0.7, scale=scale)
        wait_image('app_img/dcfj_challenge/3_1.png', threshold=0.8, scale=scale)
        if is_image_now('app_img/dcfj_challenge/3_1.png', threshold=0.8, scale=scale):
            break
    if_exist_click_image('app_img/public_img/yes.png', threshold=0.7, scale=scale)

    while True:
        wait_click_image('app_img/dcfj_challenge/4.png', threshold=0.7, scale=scale)
        wait_click_image('app_img/dcfj_challenge/5.png', threshold=0.7, scale=scale)
        
        if not is_image_now('app_img/dcfj_challenge/6.png', threshold=0.7, scale=scale):
            break

        while if_exist_click_image('app_img/dcfj_challenge/6.png', threshold=0.7, scale=scale):
            wait_click_image('app_img/public_img/yes.png', threshold=0.7, scale=scale)
            wait_image('app_img/dcfj_challenge/7.png', threshold=0.6, scale=scale)
        click_image('app_img/public_img/close.png', threshold=0.7, scale=scale)